package cn.lyn4ever.order.service.impl;

import cn.lyn4ever.common.MyApiErrors;
import cn.lyn4ever.common.R;
import cn.lyn4ever.entity.OrderDetail;
import cn.lyn4ever.entity.Orders;
import cn.lyn4ever.entity.Store;
import cn.lyn4ever.order.common.OrderVal;
import cn.lyn4ever.order.feign.IGoodsServiceFeign;
import cn.lyn4ever.order.mapper.OrderDetailMapper;
import cn.lyn4ever.order.mapper.OrdersMapper;
import cn.lyn4ever.order.service.OrdersService;
import cn.lyn4ever.tool.Assert;
import cn.lyn4ever.tool.Tool;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * (Orders)表服务实现类
 *
 * @author lyn4ever
 * @since 2020-04-11 21:47:29
 */
@Service("ordersService")
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Resource
    private OrdersMapper ordersMapper;

    @Resource
    private OrderDetailMapper orderDetailMapper;

    @Resource
    private IGoodsServiceFeign iGoodsServiceFeign;
    @Value("${page.limit}")
    private Integer pageLimit;

    @Override
    public Map<String, Integer> getCountById(Integer userid) {
        return ordersMapper.getCountById(userid);
    }

    /**
     * 获取指定类型的订单列表（分页）
     *
     * @param userid
     * @param type
     * @param page
     * @param limit
     * @return
     */
    @Override
    public List<Orders> getOrders(String userid, String type, Integer page, Integer limit) {
        if (page == null || page == 0) {
            page = 1;
        }
        if (limit == null || limit == 0) {
            limit = pageLimit;
        }

        Page<Orders> pageobj = new Page<Orders>(page, limit);
        QueryWrapper<Orders> wrapper = new QueryWrapper<Orders>();
        wrapper.eq("userid", userid);
        if (StringUtils.isNotBlank(type) && !StringUtils.equals(type, OrderVal.ORDER_HAS_DELETED)) {
            wrapper.eq("zt", type);
        }
        wrapper.orderByAsc("update_time");
        Page<Orders> ordersPage = ordersMapper.selectPage(pageobj, wrapper);
        List<Orders> records = ordersPage.getRecords();
        //查询所有的订单详情
        if (records.size() > 0) {
            records.forEach(orders -> {
                QueryWrapper<OrderDetail> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("lsh", orders.getLsh());
                List<OrderDetail> orderDetails = orderDetailMapper.selectList(queryWrapper);
                if (orderDetails.size() > 0) {
                    orders.setOrderDetails(orderDetails);
                }
            });
        }
        return records;
    }

    /**
     * 查询单个订单的详情
     *
     * @param lsh
     * @return
     */
    @Override
    public Orders getOneDetail(String lsh) {
        QueryWrapper<Orders> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("lsh", lsh);
        Orders orders = ordersMapper.selectOne(wrapper1);
        Assert.NotNull(orders, MyApiErrors.FAILED,"没有查询到结果");

        QueryWrapper<OrderDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("lsh", orders.getLsh());
        List<OrderDetail> orderDetails = orderDetailMapper.selectList(wrapper);
        orders.setOrderDetails(orderDetails);

        // 查询店铺相关信息
        R storeById = iGoodsServiceFeign.getStoreById(orders.getStoreid());
        if (StringUtils.equals(storeById.getCode(), "200")) {
            Store data = (Store) Tool.formatObj(storeById.get("data"), Store.class);
            orders.setStoreName(data.getTitle());
        }
        return orders;
    }

    /**
     * 删除单个订单
     *
     * @param lsh
     * @return
     */
    @Override
    public int delOne(String lsh) {
        Orders orders = new Orders();
        orders.setLsh(lsh);
        orders.setZt(OrderVal.ORDER_HAS_DELETED);
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("lsh", lsh);
        //只有已完成订单和待会付款定单可以删除
        ArrayList list = new ArrayList();
        list.add(OrderVal.ORDER_WAIT_PAY);
        list.add(OrderVal.ORDER_HAS_CANCLE);
        list.add(OrderVal.ORDER_END);
        wrapper.in("zt",list);
        return ordersMapper.update(orders, wrapper);
    }


}